<template>
    <el-row>
        <el-col :span="24">
            <el-card shadow="never">
                <VueEcharts style="width: 100%; height: 300px;" :option="ordersOption"></VueEcharts>
            </el-card>
        </el-col>
    </el-row>

    <el-row :gutter="14" style="margin-top: 14px;">
        <el-col :span="12">
            <el-card shadow="never">
                <VueEcharts style="width: 100%; height: 300px;" :option="usersOption"></VueEcharts>
            </el-card>
        </el-col>
        <el-col :span="12">
            <el-card shadow="never">
                <VueEcharts style="width: 100%; height: 300px;" :option="userff"></VueEcharts>
            </el-card>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import api from '@/api';
import { computed, onMounted, ref } from 'vue';

const usersData = ref({
    count: [],
    date: []
})



const usersOption = computed(() => {
    return {
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: usersData.value.date,
            // 设置x轴的文字倾斜和文字大小
            axisLabel: { interval: 0, rotate: 30, fontSize: 10 },
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: usersData.value.count,
                type: 'line',
                // 折线图变成曲线图
                smooth: true,
                // 去掉线条上的小圆点
                symbol: "none",
                // 给曲线图下方添加背景颜色
                areaStyle: {
                    color: "skyblue",
                },
                lineStyle: {
                    color: "skyblue",
                },
            }
        ]
    }
})




const ordersData = ref({
    date: [],
    orderCount: [],
    orderMoney: []
})
const ordersOption = computed(() => {
    return {
        tooltip: {
            trigger: "axis",
            axisPointer: {
                type: "cross",
                crossStyle: {
                    color: "#999",
                },
            },
        },
        grid: {
            left: 30,
            right: 35,
            bottom: 30,
        },
        legend: {
            data: ["订单数量", "订单金额"],
        },
        xAxis: [
            {
                type: "category",
                axisLabel: { interval: 0, rotate: 30 },
                data: ordersData.value.date,
                axisPointer: {
                    type: "shadow",
                },
            },
        ],
        yAxis: [
            {
                type: "value",
                name: "数量",
                // min: 0,
                // max: 250,
                // interval: 50,
                // axisLabel: {
                //     formatter: '{value}'
                // }
            },
            {
                type: "value",
                name: "金额",
                // min: 0,
                // max: 25,
                // interval: 5,
                // axisLabel: {
                //     formatter: '{value} °C'
                // }
            },
        ],
        series: [
            {
                name: "订单数量",
                type: "bar",
                tooltip: {
                    valueFormatter: function (value: string) {
                        return value + " ml";
                    },
                },
                data: ordersData.value.orderCount,
            },
            {
                name: "订单金额",
                type: "line",
                // smooth: true, // 折线变曲线
                yAxisIndex: 1,
                tooltip: {
                    valueFormatter: function (value: string) {
                        return value + " °C";
                    },
                },
                data: ordersData.value.orderMoney,
            },
        ],
    };
})



const hasData = ref({
    counts: [],
    dates: []
})




const userff = computed(() => {
    return {

        title: {
            // text: 'Referer of a Website',
            subtext: '购买用户统计',
            right: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            right: 'right'
        },
     
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: '50%',
                data: [
                    { value: 9048, name: '未消费用户' },
                    { value: 635, name: '消费一次用户' },
                    { value: 80, name: '留存客户' },
                    { value: 44, name: '回流客户' },
                    // { value: 300, name: 'Video Ads' }
                ],
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]




    }
})
















onMounted(() => {
    getUsersEchartsData();
    getOrdersEchartsData();
    getHasdd();
})

const getUsersEchartsData = () => {
    api.home.getUsers()
        .then(res => {
            if (res.code == 200) {
                usersData.value = res.data;
            }
        })
}
const getOrdersEchartsData = () => {
    api.home.getOrders({
        cycle: 'thirtyday'
    })
        .then(res => {
            if (res.code == 200) {
                ordersData.value = res.data;
            }
        })
}


const getHasdd = () => {
    api.home.getHas()
        .then(res => {
            if (res.code == 200) {
                hasData.value = res.data;
            }
        })
}



</script>

<style scoped></style>